package pers.huangyuhui.sms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.huangyuhui.sms.bean.Grade;
import pers.huangyuhui.sms.service.GradeService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("/grade")
public class GradeController {

    // 注入业务对象
    @Autowired
    private GradeService gradeService;

    // 存储预返回页面的结果对象
    private Map<String, Object> result = new HashMap<>();


    @GetMapping("/goGradeListView")
    public String goGradeListPage() {
        return "grade/gradeList";
    }


    @PostMapping("/getGradeList")
    @ResponseBody
    public Map<String, Object> getGradeList(Integer page, Integer rows, String introducation) {

        //注意:使用Java Bean传递gradename,防止以下异常 !
        //org.springframework.web.util.NestedServletException: Request processing failed;
        // nested exception is org.mybatis.spring.MyBatisSystemException:
        // nested exception is org.apache.ibatis.reflection.ReflectionException:
        // There is no getter for property named 'name' in 'class java.lang.String'
        Grade grade = new Grade();
        grade.setIntroducation(introducation);

        //设置每页的记录数
        PageHelper.startPage(page, rows);
        //根据年级名称获取指定或全部年级信息列表
        List<Grade> list = gradeService.selectList(grade);
        //封装信息列表
        PageInfo<Grade> pageInfo = new PageInfo<>(list);
        //获取总记录数
        long total = pageInfo.getTotal();
        //获取当前页数据列表
        List<Grade> gradeList = pageInfo.getList();
        //存储数据对象
        result.put("total", total);
        result.put("rows", gradeList);

        return result;
    }






}